﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

public class UseableObject:HeavyObject
{
    protected Animation useable;
    protected Animation enter;
    protected new UseableObject.Type thisType;
    protected bool isFull = false;//tracks whether or not the player is in here
    /**
     * creates a CollidableObject at 'placement' of type 'type'.
     * initializes origin, and
     * type. This will set the SpawnPointer for this instance
     * to null.
     * @param type an instance of ObjectFactory.TypeEnum which
     * specifies what type of CollidableObject to create
     * @param placement the world x,y,z coordinates that define
     * where this instance is placed.
     * @see ObjectFactory.TypeEnum
     */
    public UseableObject(UseableObject.Type type, Vector3 placement)
        : base(type, placement)
    {
        init(type, placement);
        thisType = type;
        useable = UseableObject.Type.getUseableAnimation();
        useable.setCurrentFrame((useable.getTotalFrames() - 1) / 2);
        //set useable to the middle frame so it is offset from grabable
        enter = new Animation("enter", 2, false, thisType.useFrames);
    }

    public void setFull(bool fullness)
    {
        isFull = fullness;
        currentAnimation = enter;
        if (isFull)
        {
            supplement = null;
        }
        else
        {
            setUseable(nowUseable);
        }
    }

    public override void setUseable(Boolean value)
    {
        nowUseable = value;
        if (!beingUsed)
        {
            if (nowUseable)
            {
                supplement = grabable;
            }
            else if (highlighted)
            {
                supplement = arrow;
            }
            else
            {
                supplement = null;
            }
        }
    }

    public override void draw()
    {
        base.draw(); 
        if (nowUseable || isFull)
        {
            useable.render(new Vector2(screenCoord.X-44, screenCoord.Y - thisType.collisionBoxDimentions.Y), screenCoord.Z + thisType.depthOffset / 2);
        }
    }
    
    public override void update(double elapsed)
    {
        base.update(elapsed);
        if (nowUseable || isFull)
        {
            useable.update(elapsed);
            if (enter.isFinished)
            {
                currentAnimation = stasis;
                enter.setCurrentFrame(0);
                enter.isFinished = false;
            }
        }
    }

    public new class Type : HeavyObject.Type
    {
        //useable objects:
        public static readonly Type Dumpster = new Type(
            ObjectFactory.TypeEnum.Dumpster,//type
            new Vector3(101, 162, 0),//origin
            new Vector3(202, 162, 100),//collision
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("dumpsterClosed"), 
                    new Rectangle(0, 0, 203, 162), 
                    0, 
                    new Vector2(101, 162))},
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("dumpsterClosed"), 
                    new Rectangle(0, 0, 203, 162), 
                    0, 
                    new Vector2(101, 162))},
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("dumpsterOpen"), 
                    new Rectangle(0, 0, 203, 215), 
                    0, 
                    new Vector2(101, 215))});
        public static readonly Type Fridge = new Type(
            ObjectFactory.TypeEnum.Fridge,//type
            new Vector3(46, 198, 0),//origin
            new Vector3(93, 198, 80),//collision
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("refrigeratorClosed"), 
                    new Rectangle(0, 0, 103, 198), 
                    0, 
                    new Vector2(51, 193))},
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("refrigeratorClosed"), 
                    new Rectangle(0, 0, 103, 198), 
                    0, 
                    new Vector2(51, 193))},
            new ArrayList { 
                new Frame(
                    Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.MISSION_OBJECT).get("refrigeratorOpen"), 
                    new Rectangle(0, 0, 151, 197), 
                    0, 
                    new Vector2(50, 193))});

        //actual class data
        protected ArrayList useFrameList;
        public ArrayList useFrames
        {
            get { return useFrameList; }
        }

        /**
         * this is a dummy constructor.
         */
        protected Type() { }

        protected Type(ObjectFactory.TypeEnum type, Vector3 origin, Vector3 collision,
            ArrayList stasisFrames, ArrayList explosionFrames, ArrayList interactFrames)
            : base(type, origin, collision, stasisFrames, explosionFrames)
        {
            useFrameList = interactFrames;
        }

        /**
         * Private constructor.
         * This private constructor ensures that only 
         * the pre-specified Type objects can be used 
         * and compared against.
         * @param origin
         * @param collision
         * @param basic
         * @param destruction
         */
        protected Type(Vector3 origin, Vector3 collision)
        {
        }

        public static new UseableObject.Type getType(ObjectFactory.TypeEnum type)
        {
            switch (type)
            {
                case ObjectFactory.TypeEnum.Dumpster: return Dumpster;
                case ObjectFactory.TypeEnum.Fridge: return Fridge;
            }
            return null;
        }

        public static Animation getUseableAnimation()
        {
            return new Animation(
                "useable",//name
                12,//framerate
                true,//loopable
                new ArrayList { 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 44)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 48)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 51)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 53)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 54)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 53)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 51)), 
                    new Frame(
                        Game.getGame().getAssetLibrary().get(AssetLibrary.assetType.USE).get("interact"), 
                        new Rectangle(0, 0, 44, 44), 
                        0, 
                        new Vector2(22, 48)) });//framelist
        }
    }
}
